package com.app.ytong.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.ytong.basis.Constants;
import com.app.ytong.greendao.GreenDaoUtils;

import my.FileUtils;
import my.LogUtil;
import my.TimeUtils;

public class BaseInfoUpdateCache extends BasisBean {

    private boolean needUpdateGoodsType = true;    //货品种类
    private boolean needUpdateGoodsLevel = true;    //货品等级
    private boolean needUpdateGoodsTime = true;    //货品时间
    private boolean needUpdateUser = true;        //用户
    private boolean needUpdateUnit = true;        //单位
    private boolean needUpdateTrader = true;        //交易员


    private int goodsTypeCount;
    private int goodsLevelCount;
    private int goodsTimeCount;
    private int userCount;
    private int unitCount;
    private int traderCount;



    private int goodsTypeCountUpdated;
    private int goodsLevelCountUpdated;
    private int goodsTimeCountUpdated;
    private int userCountUpdated;
    private int unitCountUpdated;
    private int traderCountUpdated;


    private boolean isForceUpdate = false;


    @JSONField(serialize = false)
    public boolean isNeedUpdate(){
        if(isForceUpdate )return true;
        LogUtil.i("-----time111---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        LogUtil.i("ContactListBean.isCacheExist()---?"+ContactListBean.isCacheExist());

        if( needUpdateGoodsType ){
            needUpdateGoodsType = (goodsTypeCount > 0);
        }

        if( needUpdateGoodsLevel ){
            needUpdateGoodsLevel = (goodsLevelCount > 0);
        }

        if( needUpdateGoodsTime ){
            needUpdateGoodsTime = (goodsTimeCount > 0);
        }

        if( needUpdateUser ){
            needUpdateUser = (userCount > 0);
        }
        if( needUpdateUnit ){
            needUpdateUnit = (unitCount > 0);
        }

        if( needUpdateTrader ){
            needUpdateTrader = (traderCount > 0);
        }


        LogUtil.i("-----time222---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        if( needUpdateGoodsType||needUpdateGoodsLevel||needUpdateGoodsTime
                ||needUpdateUser||needUpdateUnit||needUpdateTrader)return true;

            return false;
    }

    @JSONField(serialize = false)
    public void setUpdateCountByType(String map_type,int  count){
        switch (map_type){
            case GreenDaoUtils.TYPE_GOODS:
                goodsTypeCount = count;
                break;
            case GreenDaoUtils.TYPE_LEVEL:
                goodsLevelCount = count;
                break;
            case GreenDaoUtils.TYPE_TIME:
                goodsTimeCount = count;
                break;
            case GreenDaoUtils.TYPE_USER:
                userCount = count;
                break;
            case GreenDaoUtils.TYPE_UNIT:
                unitCount = count;
                break;
            case GreenDaoUtils.TYPE_TRADER:
                traderCount = count;
                break;
        }
    }

    @JSONField(serialize = false)
    public boolean isUpdateSuccess(){
        if( (goodsTypeCountUpdated == 0) &&(goodsLevelCountUpdated == 0) &&(goodsTimeCountUpdated == 0)
                && (userCountUpdated == 0)&& (unitCountUpdated == 0)&& (traderCountUpdated == 0)){
            return false;
        }
        return true;
    }


    @JSONField(serialize = false)
    public void setUpdatedCountByMaptype(String type,int updatedCount){
        switch (type){
            case GreenDaoUtils.TYPE_GOODS:
                goodsTypeCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_LEVEL:
                goodsLevelCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_TIME:
                goodsTimeCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_USER:
                userCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_UNIT:
                unitCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_TRADER:
                traderCountUpdated = updatedCount;
                break;
        }
    }


    @JSONField(serialize = false)
    public void setAllNeedUpdate(boolean isNeed){
        needUpdateGoodsType = isNeed;
        needUpdateGoodsLevel = isNeed;
        needUpdateGoodsTime = isNeed;
        needUpdateUser = isNeed;
        needUpdateUnit = isNeed;
        needUpdateTrader = isNeed;
    }


    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "tea_ytong_baseinfo_update_cache_.data";

    @JSONField(serialize = false)
    public static BaseInfoUpdateCache getFromCache() {
        BaseInfoUpdateCache data = (BaseInfoUpdateCache) FileUtils.readObject(FILE_CACHE);
        if (data == null) {
            data = new BaseInfoUpdateCache();
        }
        return data;
    }

    @JSONField(serialize = false)
    public void save() {
        isForceUpdate =false;
        FileUtils.cacheObject(FILE_CACHE, this);
    }
    @JSONField(serialize = false)
    public static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
    }



    public boolean isForceUpdate() {
        return isForceUpdate;
    }

    public void setForceUpdate(boolean forceUpdate) {
        isForceUpdate = forceUpdate;
    }

    public boolean isNeedUpdateGoodsType() {
        return needUpdateGoodsType;
    }

    public void setNeedUpdateGoodsType(boolean needUpdateGoodsType) {
        this.needUpdateGoodsType = needUpdateGoodsType;
    }

    public int getGoodsTypeCount() {
        return goodsTypeCount;
    }

    public void setGoodsTypeCount(int goodsTypeCount) {
        this.goodsTypeCount = goodsTypeCount;
    }

    public int getGoodsTypeCountUpdated() {
        return goodsTypeCountUpdated;
    }

    public void setGoodsTypeCountUpdated(int goodsTypeCountUpdated) {
        this.goodsTypeCountUpdated = goodsTypeCountUpdated;
    }

    public boolean isNeedUpdateUnit() {
        return needUpdateUnit;
    }

    public void setNeedUpdateUnit(boolean needUpdateUnit) {
        this.needUpdateUnit = needUpdateUnit;
    }

    public int getUnitCount() {
        return unitCount;
    }

    public void setUnitCount(int unitCount) {
        this.unitCount = unitCount;
    }

    public int getUnitCountUpdated() {
        return unitCountUpdated;
    }

    public void setUnitCountUpdated(int unitCountUpdated) {
        this.unitCountUpdated = unitCountUpdated;
    }


    public boolean isNeedUpdateUser() {
        return needUpdateUser;
    }

    public void setNeedUpdateUser(boolean needUpdateUser) {
        this.needUpdateUser = needUpdateUser;
    }

    public int getUserCount() {
        return userCount;
    }

    public void setUserCount(int userCount) {
        this.userCount = userCount;
    }

    public int getUserCountUpdated() {
        return userCountUpdated;
    }

    public void setUserCountUpdated(int userCountUpdated) {
        this.userCountUpdated = userCountUpdated;
    }

    public boolean isNeedUpdateGoodsLevel() {
        return needUpdateGoodsLevel;
    }

    public void setNeedUpdateGoodsLevel(boolean needUpdateGoodsLevel) {
        this.needUpdateGoodsLevel = needUpdateGoodsLevel;
    }

    public boolean isNeedUpdateGoodsTime() {
        return needUpdateGoodsTime;
    }

    public void setNeedUpdateGoodsTime(boolean needUpdateGoodsTime) {
        this.needUpdateGoodsTime = needUpdateGoodsTime;
    }

    public int getGoodsLevelCount() {
        return goodsLevelCount;
    }

    public void setGoodsLevelCount(int goodsLevelCount) {
        this.goodsLevelCount = goodsLevelCount;
    }

    public int getGoodsTimeCount() {
        return goodsTimeCount;
    }

    public void setGoodsTimeCount(int goodsTimeCount) {
        this.goodsTimeCount = goodsTimeCount;
    }

    public int getGoodsLevelCountUpdated() {
        return goodsLevelCountUpdated;
    }

    public void setGoodsLevelCountUpdated(int goodsLevelCountUpdated) {
        this.goodsLevelCountUpdated = goodsLevelCountUpdated;
    }

    public int getGoodsTimeCountUpdated() {
        return goodsTimeCountUpdated;
    }

    public void setGoodsTimeCountUpdated(int goodsTimeCountUpdated) {
        this.goodsTimeCountUpdated = goodsTimeCountUpdated;
    }


    public boolean isNeedUpdateTrader() {
        return needUpdateTrader;
    }

    public void setNeedUpdateTrader(boolean needUpdateTrader) {
        this.needUpdateTrader = needUpdateTrader;
    }

    public int getTraderCount() {
        return traderCount;
    }

    public void setTraderCount(int traderCount) {
        this.traderCount = traderCount;
    }

    public int getTraderCountUpdated() {
        return traderCountUpdated;
    }

    public void setTraderCountUpdated(int traderCountUpdated) {
        this.traderCountUpdated = traderCountUpdated;
    }

}
